package com.yitiao.data_algorithms.leetcode.editor.cn;

import com.yitiao.data_algorithms.leetcode.editor.cn.base.ListNode;
import com.yitiao.data_algorithms.leetcode.editor.cn.base.TreeNode;

import java.util.ArrayList;
import java.util.List;

class ZigzagConversion {
    // 2022-08-30 12:53:08
    public static void main(String[] args) {
        Solution solution = new ZigzagConversion().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String convert(String s, int numRows) {
            if (numRows == 1) return s;
            List<StringBuilder> list = new ArrayList<>();
            for (int i = 0; i < numRows; i++) {
                list.add(new StringBuilder());
            }
            // 在转弯处调整 list 的下标
            int index = 0, flag = -1;
            for (int i = 0; i < s.length(); i++) {
                list.get(index).append(s.charAt(i));
                if (index == 0 || index == numRows - 1) {
                    flag = -flag;
                }
                index += flag;
            }
            StringBuilder ans = list.get(0);
            for (int i = 1; i < list.size(); i++) {
                ans.append(list.get(i));
            }
            return ans.toString();

        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}